package com.example.demo.controller;


import cn.dev33.satoken.util.SaResult;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.demo.dto.StudentRequestDTO;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.Student;
import com.example.demo.mapper.entity.StudentView;
import com.example.demo.service.StudentManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import com.example.demo.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api("学生信息接口")
@RequestMapping("studentManage")
@RestController
@Slf4j
public class StudentManageController {
    @Autowired
    private StudentManageService service;


    @ResponseBody
    @ApiOperation(value = "获取学生列表接口", notes = "需要加入session")
    @RequestMapping(value = "getStudentList", method = {RequestMethod.POST})
    public Result<PageVo<StudentView>> getList(@RequestBody PageBo pageBo) {
        PageVo<StudentView> list = service.getStudentList(pageBo);
        return Result.ok(list);
    }

    //按照学号，籍贯，民族，学院，专业，宿舍楼
   /* @ResponseBody
    @ApiOperation(value = "搜索日志列表接口", notes = "加入session")
    @RequestMapping(value = "search", method = {RequestMethod.POST})
    public Result<PageVo<StudentView>> getListSearch(@RequestBody StudentRequestDTO request) {
        // if (StpUtil.getLoginIdDefaultNull() != null) {
        String studentNum=request.getNumber();
        String name=request.getName();
        String college=request.getCollegeName();
        String major=request.getMajorName();


        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<StudentView> logs = service.searchStudent(studentNum,name,college,major,pageBo);
        return Result.ok(logs);
      /*  }else {
            return Result.warn("未登录");
        }

    }*/
    //按照学号，籍贯，民族，学院，专业，宿舍楼
    @ResponseBody
    @ApiOperation(value = "搜索日志列表接口", notes = "加入session")
    @RequestMapping(value = "search", method = {RequestMethod.POST})
    public Result<PageVo<StudentView>> getListSearch(@RequestBody StudentRequestDTO request) {
        // if (StpUtil.getLoginIdDefaultNull() != null) {
        String studentNum=request.getNumber();
        String name=request.getName();
        String college=request.getCollegeName();
        String major=request.getMajorName();

        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<StudentView> logs = service.searchStudent(studentNum,name,college,major,pageBo);
        return Result.ok(logs);
      /*  }else {
            return Result.warn("未登录");
        }*/

    }

    @ResponseBody
    @ApiOperation("删除学生接口")
    @RequestMapping(value = "delete", method = {RequestMethod.POST})
    public Result<String> delete(@RequestBody StudentView student) {
        service.deleteStudent(student);
        return Result.ok("删除成功");
    }


    @ResponseBody
    @ApiOperation("批量删除接口")
    @RequestMapping(value ="deleteList", method = {RequestMethod.POST})
    public Result<String> deleteList(@RequestBody List<StudentView> studentList) {
        for (StudentView user : studentList) {
            System.out.println(user);
        }
        service.deleteList(studentList);
        return Result.ok("删除成功");
    }



    @ResponseBody
    @ApiOperation("修改学生接口")
    @RequestMapping(value = "update", method = {RequestMethod.POST})
    public Result<String> updateUserInfo(@RequestBody StudentRequestDTO request) {

        String college = request.getCollegeName();
        String major = request.getMajorName();

        return service.updateStudent(request,college,major);
    }


   /* @ResponseBody
    @RequestMapping(value = "add", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("用户新增接口")
    public Result<String> addUser(@RequestBody StudentRequestDTO request) {

        String college = request.getCollegeName();
        String major = request.getMajorName();

        return service.addStudent(request,college,major);

    }*/

    @ResponseBody
    @RequestMapping(value = "add", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("用户新增接口")
    public SaResult addUser(@RequestBody StudentRequestDTO request) {
            // 获取当前会话账号id, 如果未登录，则返回 null
        String college = request.getCollegeName();
        String major = request.getMajorName();

        service.addStudent(request,college,major);

        return SaResult.ok();
    }


    @ApiOperation("数据导入接口")
    @PostMapping("import")
    public Result importData(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename(); // 获取文件名
        List<Student> studentList = new ArrayList<>();  //定义studentList

        if (originalFilename.toLowerCase().endsWith(".csv")) {
            service.csvImportAsync(file);
        } else if (originalFilename.toLowerCase().endsWith(".xls") || originalFilename.toLowerCase().endsWith(".xlsx")) {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            studentList = reader.readAll(Student.class);
            String initialPassword = "abc123";
            studentList.forEach(student -> student.setPassword(MD5Util.encryptPassword(initialPassword)));
            try {
                service.saveData(studentList);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(400, "数据批量导入错误");
            }
        } else {
            return Result.error(400, "不支持该文件格式");
        }
        return Result.ok();
    }



}
